import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login";
import Index from "../components/Index";
import MainIndex from "../components/MainIndex";
import ChainIndex from "../components/ChainIndex";
import MyCenterIndex from "../components/MyCenterIndex";
import PowerIndex from "../components/PowerIndex";
import EmpIndex from "../components/EmpIndex";
import DepIndex from "../components/DepIndex";
import PositionChainIndex from "../components/PositionChainIndex";
import ErrorPage from "../components/ErrorPage";
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueRouter);
Vue.use(VueAxios, axios);

const router = new VueRouter({
  /*base: '/mycli/',*/
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'login',
      meta:{
        title: '宠达CRM系统 — 登录 (・(ｪ)・)'
      },
      component: Login
    },
    {
      path: '/index',
      name: 'index',
      meta:{
        requireAuth: true,
        title: '宠达CRM系统 — 首页 (・(ｪ)・)'
      },
      component: Index,
      children:[
        {
          path: 'mycenter',
          name: 'mycenter',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 个人中心 (・(ｪ)・)'
          },
          component: MyCenterIndex
        },
        {
          path: 'main',
          name: 'main',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 首页 (・(ｪ)・)'
          },
          component: MainIndex
        },
        {
          path: 'chain',
          name: 'chain',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 分店管理 (・(ｪ)・)'
          },
          component: ChainIndex
        },
        {
          path: 'power',
          name: 'power',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 权限管理 (・(ｪ)・)'
          },
          component: PowerIndex
        },
        {
          path: 'emp',
          name: 'emp',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 员工管理 (・(ｪ)・)'
          },
          component: EmpIndex
        },
        {
          path: 'dep',
          name: 'dep',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 部门管理 (・(ｪ)・)'
          },
          component: DepIndex
        },
        {
          path: 'pos',
          name: 'pos',
          meta: {
            requireAuth: true,
            title: '宠达CRM系统 — 职位管理 (・(ｪ)・)'
          },
          component: PositionChainIndex
        },
        {
          path: 'error',
          name: 'error',
          meta:{
            title: '宠达CRM系统 — 未知领域 (・(ｪ)・)'
          },
          component: ErrorPage
        }

      ]
    }
  ]
});



// 验证是否登录，登录拦截
router.beforeEach((to,from,next)=>{
  if (to.matched.length === 0) {  //如果未匹配到路由
    from.name ? next({ name:from.name }) : next('/index/error');   //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  } else {
    if (to.matched.some(res => res.meta.requireAuth)) { // 判断是否需要登录权限

      axios.post("/api/check",{passID: sessionStorage.getItem('passID')})
        .then(function (response) {

          if (response.data.statusCode == "200") {
            next();
          } else {
            sessionStorage.removeItem('passID');
            next({
              path: '/',
              query: {redirect: to.fullPath}
            })
          }
        });
    } else {
      next();
    }
  }
});

// 更改页面的名字
router.beforeEach((to,from,next)=>{
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
});

/**
 * 重写路由的push方法, 就可以访问子路由
 */
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
};



export default router
